package org.example.dingtalkintegrationservice.dingtalk.processTask;

import jakarta.annotation.Resource;
import org.example.dingtalkintegrationservice.exception.DingTalkCallExceptionGroovy;
import org.example.dingtalkintegrationservice.pojo.IdRecord;
import org.example.dingtalkintegrationservice.repository.IdRecordRepository;
import org.example.dingtalkintegrationservice.service.IdRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.aliyun.dingtalkworkflow_1_0.Client;
import com.aliyun.dingtalkworkflow_1_0.models.UpdateIntegratedTaskHeaders;
import com.aliyun.dingtalkworkflow_1_0.models.UpdateIntegratedTaskRequest;
import com.aliyun.dingtalkworkflow_1_0.models.UpdateIntegratedTaskResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.Collections;
import java.util.Objects;

/**
 * @author pc
 */
@Component
public class UpdateProcessTask {
@Autowired
IdRecordService idRecordService;
@Resource
    IdRecordRepository idRecordRepository;
    static Client flowClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        try{
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
    static UpdateIntegratedTaskResponse updateTask(String token, String processInstanceId, Long taskId, String status, String result) throws Exception {
        UpdateIntegratedTaskHeaders updateIntegratedTaskHeaders = new UpdateIntegratedTaskHeaders()
                .setXAcsDingtalkAccessToken(token);
        UpdateIntegratedTaskRequest.UpdateIntegratedTaskRequestTasks tasks0 = new UpdateIntegratedTaskRequest.UpdateIntegratedTaskRequestTasks()
                .setTaskId(taskId)
                .setStatus(status)
                .setResult(result);
        UpdateIntegratedTaskRequest updateIntegratedTaskRequest = new UpdateIntegratedTaskRequest()
                .setProcessInstanceId(processInstanceId)
                .setTasks(Collections.singletonList(
                        tasks0
                ));
        return flowClient().updateIntegratedTaskWithOptions(updateIntegratedTaskRequest, updateIntegratedTaskHeaders, new RuntimeOptions());
    }

    static String updateTaskFromPlm(String token, String processInstanceId, String taskId, String res) throws Exception {
        Long taskId0 =Long.parseLong(taskId);
        String result= Objects.equals(res, "1") ?"AGREE":"REFUSE";
//    return JSON.toJSONString(updateTask(token,processInstanceId,taskId0,"COMPLETED",result))
        var updateRes=updateTask(token,processInstanceId,taskId0,"COMPLETED",result);
        if (updateRes.statusCode!=200){
            throw new DingTalkCallExceptionGroovy(updateRes.body.toString());
        }
        return JSON.toJSONString(updateRes.body);
    }
public String exec(String token,String flowId,String plmTaskId,String userId,String res) throws Exception {
        String processInstanceId=idRecordService.getId2ById1(flowId);
        String taskId;
        if (userId!=null){
            taskId=idRecordService.getId2ById1AndUserId(plmTaskId,userId);
        }
        else{
            taskId=idRecordService.getId2ById1(plmTaskId);
        }
        String output = updateTaskFromPlm(token, processInstanceId, taskId, res);
        IdRecord idRecord=new IdRecord();
    idRecord.setId1(plmTaskId);
    idRecord.setId2(taskId);
    idRecord.setParentId(processInstanceId);
    idRecord.setOperate("update");
    idRecord.setUserId(userId);
    idRecord.setTag("/updateProcessTask");
    idRecord.setExt(res);
        idRecordRepository.save(idRecord);
    return output;
}
}